﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using ProgramDioptase.ItemTypes;

namespace ProgramDioptase
{
    public static class ElementRenderer
    {
        public static void AddGenresToPanel(Type enumeration, Panel panel)
        {
            var genres = Enum.GetValues(enumeration);

            foreach (var genre in genres)
            {
                var currentGenreCheckBox = new CheckBox();
                currentGenreCheckBox.Content = genre;
                currentGenreCheckBox.Margin = new Thickness(3, 2, 2, 0);

                panel.Children.Add(currentGenreCheckBox);
            }
        }

        public static void VisualizeAllElementsPanel<T>(Panel panel, IList<T> itemsInfo, Uri baseDirectory)
            where T : CatalogItem
        {
            panel.Children.Clear();

            for (int k = 0; k < 1; k++)
            {
                for (int i = 0; i < itemsInfo.Count; i++)
                {
                    var nestedPanel = new StackPanel();
                    nestedPanel.Margin = new Thickness(10, 0, 0, 20);
                    nestedPanel.Height = 200;
                    nestedPanel.Width = 200;

                    var image = new Image();
                    image.Height = 180;
                    image.MaxHeight = 180;
                    image.Width = 180;
                    image.MaxWidth = 180;
                    image.Stretch = System.Windows.Media.Stretch.UniformToFill;

                    // Loading an image
                    LoadImage(baseDirectory, itemsInfo[i].Name, image);

                    // Add image to Stack Panel
                    nestedPanel.Children.Add(image);

                    // Loading other components as Name, Genre, Year etc.
                    PushItemsInPanel(nestedPanel, itemsInfo[i]);

                    // Set event args
                    var eventInfo = itemsInfo[i];

                    // Set Mouse Click event to Image
                    nestedPanel.MouseLeftButtonDown += (sender, args) =>
                    {
                        var catalogItemInfo = new InformationAboutCatalogItem(eventInfo, baseDirectory);
                        catalogItemInfo.ShowDialog();
                    };

                    panel.Children.Add(nestedPanel);
                }
            }
        }

        private static void PushItemsInPanel(Panel panel, CatalogItem itemInfo)
        {
            var textBlock = new TextBlock();
            textBlock.TextWrapping = new TextWrapping();
            textBlock.TextAlignment = TextAlignment.Center;
            textBlock.Text = itemInfo.Name;

            textBlock.FontSize = 15;
            panel.Children.Insert(0, textBlock);
        }

        public static void VisualizeImageInPanel(Panel panel, CatalogItem itemInfo, Uri baseDirectory)
        {
            panel.Children.Clear();

            var image = new Image();
            image.Height = 230;
            image.MaxHeight = 230;

            // Loading an image
            LoadImage(baseDirectory, itemInfo.Name, image);

            panel.Children.Add(image);
        }

        private static void LoadImage(Uri baseDirectory, string itemDirectory, Image image)
        {
            if (File.Exists(string.Format(@"{0}\{1}\image.jpg", baseDirectory, itemDirectory)))
            {
                Uri uri = new Uri(string.Format(@"{0}\{1}\image.jpg",
                    baseDirectory, itemDirectory), UriKind.RelativeOrAbsolute);

                image.Source = BitmapFrame.Create(uri);
            }
            else if (File.Exists(string.Format(@"../../{0}\no-image.png", DataManager.ResourcePath)))
            {
                Uri uri = new Uri(string.Format(@"../../{0}\no-image.png",
                    DataManager.ResourcePath), UriKind.RelativeOrAbsolute);

                image.Source = BitmapFrame.Create(uri);
            }
        }

        #region [Change content of SearchBox & SearchButton container]

        public static void VizualizeNewSearchResultByName<T>(Panel allItemsPanel, TextBox searchBox, IList<T> items, Uri baseDirectory)
            where T : CatalogItem
        {
            string searchedText = searchBox.Text.ToLower();
            
            if (!string.IsNullOrEmpty(searchedText) && !searchedText.Equals("search by name"))
            {
                List<T> result = items.ToList().FindAll(item => item.Name.ToLower().Contains(searchedText));
                ElementRenderer.VisualizeAllElementsPanel(allItemsPanel, result, baseDirectory);
            }
            else
            {
                ElementRenderer.VisualizeAllElementsPanel(allItemsPanel, items, baseDirectory);
            }
        }

        public static void VizualizeNewSearchResultByGenre<T>(Panel allItemsPanel, TextBox genreBox, IList<T> items, Uri baseDirectory)
            where T : CatalogItem
        {
            string searchedGenre = genreBox.Text.ToLower();

            if (!string.IsNullOrEmpty(searchedGenre) && !searchedGenre.Equals("search by genre"))
            {
                List<T> result = new List<T>();

                foreach (var item in items)
                {
                    foreach (var genre in item.Genres)
                    {
                        if (genre.ToLower().Contains(searchedGenre))
                        {
                            result.Add(item);
                            break;
                        }
                    }
                }

                ElementRenderer.VisualizeAllElementsPanel(allItemsPanel, result, baseDirectory);
            }
            else
            {
                ElementRenderer.VisualizeAllElementsPanel(allItemsPanel, items, baseDirectory);
            }
        }

        public static void VizualizeNewSearchResultByYear<T>(Panel allItemsPanel, TextBox yearBox, IList<T> items, Uri baseDirectory)
            where T : CatalogItem
        {
            int searchedYear = 0;
            int.TryParse(yearBox.Text, out searchedYear);

            if (searchedYear != 0 && !searchedYear.Equals("search by year"))
            {
                List<T> result = items.ToList().FindAll(item => item.ReleaseYear.Contains(searchedYear.ToString()));

                ElementRenderer.VisualizeAllElementsPanel(allItemsPanel, result, baseDirectory);
            }
            else
            {
                ElementRenderer.VisualizeAllElementsPanel(allItemsPanel, items, baseDirectory);
            }
        }

        public static void SearchBoxRemoveContent(TextBox searchBox)
        {
            searchBox.Text = string.Empty;
        }
        
        public static void SearchBoxPutContent(TextBox searchBox)
        {
            if (string.IsNullOrEmpty(searchBox.Text))
            {
                searchBox.Text = "Search by name";
            }
        }
        
        #endregion
    }
}